package com.haoyeteng.sxbd.modular.business.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.haoyeteng.sxbd.core.base.controller.BaseController;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.beans.factory.annotation.Autowired;
import com.haoyeteng.sxbd.core.base.model.ResponseResult;
import com.haoyeteng.sxbd.core.base.tips.SuccessTip;
import com.haoyeteng.sxbd.core.common.annotion.Permission;
import com.haoyeteng.sxbd.core.common.constant.factory.ConstantFactory;
import com.haoyeteng.sxbd.core.common.constant.factory.PageFactory;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import com.haoyeteng.sxbd.core.log.LogObjectHolder;
import com.haoyeteng.sxbd.core.shiro.ShiroKit;

import org.springframework.web.bind.annotation.RequestParam;

import com.haoyeteng.sxbd.modular.business.dao.MsgContentMapper;
import com.haoyeteng.sxbd.modular.business.model.MsgContent;
import com.haoyeteng.sxbd.modular.business.service.IMsgContentService;
import com.haoyeteng.sxbd.modular.business.warpper.MsgContentWarpper;

/**
 * 消息内容控制器
 *
 * @author haoyeteng
 * @Date 2019-06-26 10:14:35
 */
@Controller
@RequestMapping("/msgContent")
public class MsgContentController extends BaseController {

    private String PREFIX = "/business/msgContent/";

    @Autowired
    private IMsgContentService msgContentService;
    
    /**
     * 跳转到消息内容首页
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "msgContent.html";
    }


    /**
     * 获取消息内容列表
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    @Permission
    public ResponseResult<Object> list(Integer msgType) {
    	Page<Map<String, Object>> page=new PageFactory<Map<String,Object>>().defaultPage();
    	if(msgType==0) {
    		 page = msgContentService.selectMapsPage(page, new EntityWrapper<MsgContent>().eq("msg_category",1).eq("create_man", ShiroKit.getAdmin().getId()).orderBy("update_time", false));
    	}else {
    		page=msgContentService.selectMapsPage(page, new EntityWrapper<MsgContent>().eq("msg_type",msgType).eq("msg_category", 1).eq("create_man", ShiroKit.getAdmin().getId()).orderBy("update_time", false));
    	}
    	ResponseResult<Object> result = new ResponseResult<Object>();
        result.setData(new MsgContentWarpper(page.getRecords()).warp());
 		result.setCount(page.getTotal());
     	result.setCode(SUCCESS_TIP.getCode());
     	result.setMsg(SUCCESS_TIP.getMessage());
 		return result;
    }
    /**
             * 批量删除消息
     * @param msgContentIds
     * @return
     */
    @RequestMapping(value="/delete")
    @ResponseBody
    @Permission
    public Object deleteMsg(String msgContentIds) {
    	Integer number=msgContentService.bathDelByIds(msgContentIds);
    	if (number>0) {
    		return SUCCESS_TIP;
    	}else {
    		return ERROR_TIP;
    	}
    }
    
    
    @RequestMapping(value="/read")
    @ResponseBody
    @Permission
    public Object readMsg(String msgContentIds) {
    	Integer number=msgContentService.redByIds(msgContentIds);
    	return number>0?new SuccessTip(ConstantFactory.me().getMsgCount()):ERROR_TIP;
    }
    
    
    @RequestMapping(value="/allread/{type}")
    @ResponseBody
    @Permission
    public Object allReadMsg(@PathVariable Integer type) {
    	MsgContent msgContent=new MsgContent();
    	msgContent.setIfRead(1);
    	if(type==0) {
    		msgContentService.update(msgContent,new EntityWrapper<MsgContent>().eq("msg_category",1));
    	}else {
    		msgContentService.update(msgContent, new EntityWrapper<MsgContent>().eq("msg_type", type).eq("if_read", 0).eq("msg_category",1));
    	}
    	return new SuccessTip(ConstantFactory.me().getMsgCount());
    }
    
}
